package org.ISCS.service.utilimpl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.ISCS.entity.version.ServiceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class ServiceReaderService {

    private final ResourceLoader resourceLoader;

    @Autowired
    public ServiceReaderService(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public List<ServiceInfo> readServicesFromJsonFile(String filePath) throws IOException {
        Resource resource = resourceLoader.getResource("file:"+filePath);
        ObjectMapper objectMapper = new ObjectMapper();
        List<ServiceInfo> services = objectMapper.readValue(resource.getInputStream(), new TypeReference<List<ServiceInfo>>() {});
        return services;
    }
}
